package com.moorror.codegenvel.view.window.codegen.domain;

import com.google.common.collect.Maps;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.moorror.codegenvel.utils.VirtualFileUtils;
import com.moorror.codegenvel.utils.WriteUiUtils;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;

/**
 * @Author jiyanghuang
 * @Date 2025/10/1 10:57
 */
public class ExtractFileDesc {

    @Getter
    @Setter
    private String fileName;

    @Getter
    @Setter
    private String fileContent;

    @Getter
    @Setter
    private Editor editor;

    @Getter
    private Map<String,Object> contextKvMap = Maps.newHashMap();

    public void fileChooser(Project project, VirtualFile virtualFile){
        if (virtualFile != null) {
            if(this.editor instanceof EditorEx){
                EditorEx editorEx = (EditorEx)this.editor;
                editorEx.setHighlighter(EditorHighlighterFactory.getInstance().createEditorHighlighter(project, virtualFile.getFileType()));
            }
            String content = VirtualFileUtils.content(project, virtualFile);
            WriteUiUtils.writeEditor(this.editor, content);
            this.setFileContent(content);
            this.setFileName(virtualFile.getName());
        }
    }

    public void addContextKey(String key, String value){
        if(!contextKvMap.containsKey(key)){
            contextKvMap.put(key, value);
        }
    }
}
